package com.mission.game.utils;

import java.io.Serializable;
import java.util.ArrayList;

import com.mission.game.R;
import com.mission.game.components.Statusable;

public class Mission implements Serializable, Statusable {

	private static final long serialVersionUID = -7124237242975865446L;

	private static final int DEFAULT_SCORE = 0;

	private boolean _isScoreUpdated;
	int _score;
	String _name;
	String _description;
	ArrayList<Task> _tasks;
	Game _game;
	Status _status;

	public Mission(int score, String name, String description, Game game) {
		super();
		_score = score;
		_name = name;
		_description = description;
		_tasks = new ArrayList<Task>();
		_game = game;
		_isScoreUpdated = true;
		_status = Status.NOT_STARTED;
	}

	public Mission(Game game) {
		this(DEFAULT_SCORE, "", "", game);
	}

	public int getScore() {
		if (!_isScoreUpdated) {
			_score = 0;
			for (Task task : _tasks) {
				_score += task.getScore();
			}
			_isScoreUpdated = true;
		}
		return _score;
	}

	public String getName() {
		return _name;
	}

	public void setName(String name) {
		_name = name;
	}

	public String getDescription() {
		return _description;
	}

	public void setDescription(String description) {
		_description = description;
	}

	// TODO: replace by use of list
	public ArrayList<Task> getTasks() {
		return _tasks;
	}

	public void setTasks(ArrayList<Task> tasks) {
		_tasks = tasks;
	}

	public void addTask(Task task) {
		_tasks.add(task);
		_isScoreUpdated = false;
	}

	public Game getGame() {
		return _game;
	}

	public void setGame(Game game) {
		_game = game;
	}

	public ArrayList<String> getTaskNames() {
		ArrayList<String> names = new ArrayList<String>();
		for (Task task : _tasks) {
			names.add(task.getName());
		}
		return names;
	}

	private int findTask(Task task) {
		return findTask(task.getName());
	}

	public boolean hasTask(Task task) {
		if (findTask(task) == Game.NOT_FOUND) {
			return false;
		} else
			return true;
	}

	public boolean hasTask(String taskName) {
		if (findTask(taskName) == Game.NOT_FOUND) {
			return false;
		} else
			return true;
	}
	
	private int findTask(String taskName) {
		for (int i = 0; i < _tasks.size(); i++) {
			if (_tasks.get(i).getName().equals(taskName)) {
				return i;
			}
		}
		return Game.NOT_FOUND;
	}

	public void updateTask(Task task) {
		int pos = findTask(task);
		if (pos != Game.NOT_FOUND) {
			_tasks.set(pos, task);
		}
		_isScoreUpdated = false;
	}

	public void removeTask(String clickedTask) {
		int pos = findTask(clickedTask);
		if (pos != Game.NOT_FOUND) {
			_tasks.remove(pos);
		}
	}

	public void updateStatus() {
		int complete = 0;
		for(Task t: _tasks)
			if (t.getStatus() == Status.COMPLETED)
				complete++;
		if (complete == 0)
			_status = Status.NOT_STARTED;
		else if (complete == _tasks.size())
			_status = Status.COMPLETED;
		else
			_status = Status.PARTIAL;
	}
	
	public void setStatus(Status status) {
		_status = status;
	}
	
	public Status getStatus() {
		return _status;
	}
	
	@Override
	public int getStatusImg() {
		switch (_status) {
		case NOT_STARTED:
			return R.drawable.star_empty;
		case PARTIAL:
			return R.drawable.star_half;
		case COMPLETED:
			return R.drawable.star_full;
		case WAITING_FOR_VERIFICATION:
			return R.drawable.test;
		case REJECTED:
			return R.drawable.erase;
		default:
			return 0;
		}
	}
}
